package com.wlq.mlsys.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wlq.mlsys.model.CourseOrder;
import com.wlq.mlsys.model.ResultJson;
import com.wlq.mlsys.service.CourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.util.List;

/**
 * @author wlq
 * @create -06-16-17:45
 */
@Controller
@RequestMapping("/courseorder")
public class CourseOrderController {

    @Autowired
    CourseOrderService courseOrderService;

    /***
     * 返回首页面
     * @return
     */
    @RequestMapping("/list")
    public String getAll(){
        return "courseorder/list";
    }

    /***
     * 根据条件查询数据，并且分页
     * @param pn
     * @param size
     * @return
     */
    @RequestMapping("/listjson")
    @ResponseBody
    public ResultJson selectCourseOrderByExample(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                         @RequestParam(value = "size",defaultValue = "10")Integer size,CourseOrder condition){
        //分页
        PageHelper.startPage(pn,size);
        List<CourseOrder> courseOrderList = courseOrderService.selectCourseOrderByExample(condition);
        PageInfo<CourseOrder> pageInfo = new PageInfo<>(courseOrderList, size);
        List<CourseOrder> data = pageInfo.getList();
        long count=pageInfo.getTotal();

        ResultJson resultJson=new ResultJson<CourseOrder>();
        ResultJson result = resultJson.successResult(null, count, data);
        return result;
    }

    /***
     * 课程订单新增返回页面
     * @return
     */
    @RequestMapping("/savePage")
    public String savePage(){
        return "courseorder/add";
    }

    /***
     * 课程订单新增到数据库
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public String save(CourseOrder courseOrder){
        return courseOrderService.insertCourseOrder(courseOrder);
    }

    /***
     * 课程订单修改数据返回页面
     * @return
     */
    @RequestMapping("/updatePage")
    public String updatePage(String uuid, Model model){
        CourseOrder courseOrder=new CourseOrder();
        courseOrder.setUuid(uuid);
        List<CourseOrder> dataList = courseOrderService.selectCourseOrderByExample(courseOrder);
        CourseOrder data=null;
        if(dataList.size()==1){
            data=dataList.get(0);
        }
        model.addAttribute("data", data);
        return "courseorder/update";
    }

    /***
     * 课程订单修改一条到数据库
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public String update(CourseOrder courseOrder){
        return courseOrderService.updateCourseOrder(courseOrder);
    }


    /***
     * 删除一条记录
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(String uuid){
        return courseOrderService.deleteCourseOrder(uuid);
    }
}
